package malus.shaofeng.com.beiwomusic.adapter;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.lidroid.xutils.view.annotation.ViewInject;
import android.view.ViewGroup.LayoutParams;
import java.util.List;

import malus.shaofeng.com.beiwomusic.R;
import malus.shaofeng.com.beiwomusic.activity.NewStoryDetailActivity;
import malus.shaofeng.com.beiwomusic.model.NewStory;
import malus.shaofeng.com.beiwomusic.utils.BitmapHelper;
import malus.shaofeng.com.beiwomusic.utils.Constants;

/**
 * Created by aaa on 15-3-11.
 */
public class NewStoryRecycleAdapter  extends RecyclerView.Adapter<NewStoryRecycleAdapter.ViewHolder> {

    private Context context;
    private List<NewStory> newStories;

    public NewStoryRecycleAdapter(Context context, List<NewStory> newStories) {
        this.context = context;
        this.newStories = newStories;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_new_story, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        View view = holder.itemView;
        final int pos=position;
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewStory story = newStories.get(pos);
                Intent intent=new Intent(context, NewStoryDetailActivity.class);
                intent.putExtra("position",pos);
                intent.putExtra("story",story);
                context.startActivity(intent);
            }
        });

        NewStory newStory = newStories.get(position);
        holder.title.setText(newStory.getTitle());
        holder.userName.setText("作者："+newStory.getUser().getNickname());
        holder.forwards.setText("转发："+newStory.getForwards());

     //   holder.image.setImageResource(R.drawable.list_empty_bg);
        DisplayMetrics metric = new DisplayMetrics();
        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;     // 屏幕宽度（像素）
        float height_img = 350.0f / 500 * (width-2*context.getResources().getDimension(R.dimen.list_img_margin));
                    LayoutParams params = holder.image.getLayoutParams();
                    params.height=(int)height_img;
                    params.width=(int)(width-2*context.getResources().getDimension(R.dimen.list_img_margin));
                    holder.image.setLayoutParams(params);
//        RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(width, height_img);
//        holder.image.setLayoutParams(param);

        String image_uri = newStory.getImage_uri();
        if (image_uri!=null&&image_uri.length()>0){
            String[] split = image_uri.split("/");
            String path="";
            for (int j = 3; j < split.length; j++) {
                path = path+split[j];
                if(j!=split.length-1){
                    path+="/";
                }
            }
            path = Constants.URL.IMAGE_URL+path;
            BitmapUtils bitmapUtils = BitmapHelper.getBitmapUtils();
            bitmapUtils.display(holder.image,path,new BitmapLoadCallBack<ImageView>() {
                @Override
                public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                    imageView.setImageBitmap(bitmap);
                }
                @Override
                public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {
                    Log.e("asd", "asd" + s);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return newStories.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        @ViewInject(R.id.item_new_story_image)
        private ImageView image;
        @ViewInject(R.id.item_new_story_title)
        private TextView title;
        @ViewInject(R.id.item_new_story_userName)
        private TextView userName;
        @ViewInject(R.id.item_new_story_forwards)
        private TextView forwards;

        public ViewHolder(View itemView) {
            super(itemView);
            ViewUtils.inject(this, itemView);
        }
    }

}
